var ease = require('ease');
var Animation = require('./animation');

function EaseAnimation(b, c, d, type) {
  Animation.call(this);
  this.name = 'ease';
  this.b = b;
  this.c = c;
  this.d = d || 500;
  this.type = type || 'outExpo';
  this.value = b;
  this.totalTime = d;
}

EaseAnimation.prototype = new Animation();

EaseAnimation.prototype.step = function() {
  var b = this.b, c = this.c, d = this.d;
  var t = Date.now() - this.startTime;
  if (b == c) {
    this.stop();
    return;
  }
  
  if (t >= d) {
    this.stop();
    return;
  }
  if ((c > b && this.value >= c) || (c < b && this.value <= c)) {
    this.stop();
    return;
  }
  var p = t / d;
  var val = ease[this.type](p);
  this.value = b + (c - b) * val;
  this.emit('step', this.value);
};

module.exports = EaseAnimation;